package com.sound.ampache.streaming;

/* Copyright (c) 2010 Brian Kirkman <kirkman.brian@gmail.com>
*
* +------------------------------------------------------------------------+
* | This program is free software; you can redistribute it and/or          |
* | modify it under the terms of the GNU General Public License            |
* | as published by the Free Software Foundation; either version 3         |
* | of the License, or (at your option) any later version.                 |
* |                                                                        |
* | This program is distributed in the hope that it will be useful,        |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
* | GNU General Public License for more details.                           |
* |                                                                        |
* | You should have received a copy of the GNU General Public License      |
* | along with this program; if not, write to the Free Software            |
* | Foundation, Inc., 59 Temple Place - Suite 330,                         |
* | Boston, MA  02111-1307, USA.                                           |
* +------------------------------------------------------------------------+
*
* Special thanks to the following: 
* Kevin Purdy, author of Amdroid, for much of the code used in this project
* 
* Jorge Ramon, author of MiamiCoder blog, for many great BlackBerry examples
* published on his blog.
* 
* 
*/


import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;

public class UrlToImage{
	private static Bitmap _bmap;
	
	public UrlToImage(String url, int imgHeight, int imgWidth){
		HttpConnection connection = null;
		InputStream inputStream = null;
		EncodedImage bitmap = null;
		byte[] dataArray = null;
		
		try{
			connection = (HttpConnection) Connector.open(url);
			inputStream = connection.openInputStream();
			byte[] responseData = new byte[10000];
			int length = 0;
			StringBuffer rawResponse = new StringBuffer();
			
			while (-1 != (length = inputStream.read(responseData)))
			{
				rawResponse.append(new String(responseData, 0, length));
			}
			
			int responseCode = connection.getResponseCode();
			if (responseCode != HttpConnection.HTTP_OK)
			{
				throw new IOException("HTTP response code: " + responseCode);
			}
			
			final String result = rawResponse.toString();
			dataArray = result.getBytes();
		}
		catch (final Exception ex){
			
		}
		
		finally{
			try{
				inputStream.close();
				inputStream = null;
				connection.close();
				connection = null;
			}
			catch(Exception e){
				
			}
		}
		
		//this if statement is used to prevent a null pointer exception
		//in the event that data is not retrieved
		if (dataArray != null){ 
			bitmap = EncodedImage.createEncodedImage(dataArray, 0,dataArray.length);
			
			// this will scale your image acc. to your height and width of bitmapfield
			int multH;
			int multW;
			int currHeight = bitmap.getHeight();
			int currWidth = bitmap.getWidth();
			multH= Fixed32.div(Fixed32.toFP(currHeight),Fixed32.toFP(imgHeight));//height
			multW = Fixed32.div(Fixed32.toFP(currWidth),Fixed32.toFP(imgWidth));//width
			bitmap = bitmap.scaleImage32(multW,multH);
			
			_bmap=bitmap.getBitmap();
		} else {
			_bmap = null;
		}
	}
	
	public Bitmap getbitmap(){
		return _bmap;
	}

}

